import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';
import { PagesService } from 'services/pages.service';
declare var layui: any;
declare var $: any;

@Component({
  selector: 'app-cp-date',
  templateUrl: './cp-date.component.html',
  styleUrls: ['./cp-date.component.scss']
})
export class CpDateComponent implements OnInit {

  constructor(public interfaceService: PagesService) { }

  @Output() private outer = new EventEmitter();
  @Input() module: any;
  @Input() data: any;
  @Input() name: any;
  @Input() title: any;
  @Input() class: any;
  @Input() css: any;
  @Input() iid: any;
  @Input() disabled: any;
  @Input() type: any;
  @Input() format: any;
  @Input() minData: any;
  @Input() maxData: any;

  style = {};
  ngOnInit() {
    this.title = this.title === undefined ? '' : this.title;
    this.disabled = this.disabled === undefined ? this.data + '_dis' : this.disabled;
    this.ngCss();
  }

  ngCss() {
    if (this.css) {
      var cssList = this.css.split(";");
      for (var i = 0; i < cssList.length; i++) {
        var cssParm = cssList[i].split(":");
        this.style[cssParm[0]] = cssParm[1];
      }
    }
  }

  inputHid = false;
  dateValue = '';
  random = new Date().getTime() + Math.random().toString(36).substr(2, 10);
  inputClick() {
    this.inputHid = true;
    setTimeout(() => {
      $('#' + this.random + ' input').click();
    }, 10);
  }

  DateClick() {
    this.module[this.data][this.name] = this.dateValue == null ? '' : this.GMTToStr(this.dateValue);
    this.inputHid = false;
    this.outer.emit(this.module[this.data][this.name]);
  }

  openDataClick(e){
    if(!e){
      this.inputHid = false;
    }
  }

  disabledDate = (startValue: Date): boolean => {
    if (!startValue) {
      return false;
    }
    if(this.minData && this.maxData){
      return startValue.getTime() < new Date(this.minData).getTime() || startValue.getTime() > new Date(this.maxData).getTime();
    }else if(this.minData){
      return startValue.getTime() < new Date(this.minData).getTime();
    }else if(this.maxData){
      return startValue.getTime() > new Date(this.maxData).getTime();
    }
  }

  resetData() {
    if (this.type!=='search') {
      this.module[this.data][this.name] = '';
      this.dateValue = '';
      this.outer.emit('');
    } else if (this.type==='search') {
      this.module[this.data][this.name] = '';
      this.dateValue = '';
      this.outer.emit('');
    }
  }

  GMTToStr(time) {
    let date = new Date(time);
    let Str;
    if (this.format === 'year') {
      Str = date.getFullYear();
    } else if (this.format === 'month') {
      Str = date.getFullYear() + '-' +
        (date.getMonth() + 1);
    } else if (this.format === 'datetime') {
      Str = date.getFullYear() + '-' +
        (date.getMonth() + 1) + '-' +
        date.getDate() + ' ' +
        date.getHours() + ':' +
        date.getMinutes() + ':' +
        date.getSeconds();
    } else {
      Str = date.getFullYear() + '-' +
        (date.getMonth() + 1) + '-' +
        date.getDate() + ' ';
    }
    return Str;
  }

  inputLayClick() {
    this.inputHid = true;
    this.dateRender();
  }
  DateLayClick(e) {
    const selectValue = this.module[this.data][this.name];
    this.inputHid = false;
    this.dateDestroy();
    this.outer.emit(selectValue);
  }

  dateRender() {
    setTimeout(() => {
      layui.use([ 'laydate'], (obj) => {
        var  laydate = layui.laydate;
        laydate.render({
          elem: '#' + this.iid
          , done: (value) => {
            this.module[this.data][this.name] = value;
          }
        });
        $('#' + this.iid).focus();
      });
    }, 50);
  }

  dateDestroy() {
    layui.use([ 'laydate'], (obj) => {
      var  laydate = layui.laydate;
      laydate.render();
    });
  }


}
